home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Examples / SystemTest.bob < prev    next >
Encoding:
Text File  |  1995-12-12  |  5.3 KB  |  400 lines  |  [TEXT/R*ch]

  1. /***
  2.  *
  3.  *    SystemTest.bob - Test the Bob compiler & interpreter
  4.  *
  5.  ***/
  6.  
  7. class Foo {        // the base class
  8.     a, b;
  9.     GetA();
  10.     GetB();
  11. }
  12.  
  13. Foo::Foo (aa, bb)
  14. {
  15.     a = aa;
  16.     b = bb;
  17.     return this;
  18. }
  19.  
  20. /*
  21. Foo::Count ( ; i)
  22. {
  23.     for (i = a; i <= b; ++i)
  24.         print(i,"\n");
  25.     return this;
  26. }
  27. */
  28.  
  29. Foo::GetA()
  30. {
  31.     return a;
  32. }
  33.  
  34. Foo::GetB ()
  35. {
  36.     return b;
  37. }
  38.  
  39.  
  40. class Bar : Foo {    // a derived class
  41.     c;
  42. }
  43.  
  44. Bar::Bar (aa, bb, cc)
  45. {
  46.     c = cc;
  47.     return Foo(aa, bb);
  48. }
  49.  
  50. Bar::GetC ()
  51. {
  52.     return c;
  53. }
  54.  
  55.  
  56. Error (test, member, value)
  57. {
  58.     print("(", test, ") ", member, " = ", value, "\n");
  59. }
  60.  
  61.  
  62. Error1 (test, aFoo)
  63. {
  64. //    print("(", test, ") aFoo->one = ", aFoo->one, "\n");
  65.     Error(test, "aFoo->a", aFoo->GetA());
  66. //    Error(test, "aFoo->one", aFoo);
  67. }
  68.  
  69.  
  70. Error2 (test, aFoo)
  71. {
  72.     Error(test, "aFoo->b", aFoo->GetB());
  73. }
  74.  
  75.  
  76. Error12 (test, aFoo, value)
  77. {
  78.     print("aFoo->a", test, "aFoo->b", " => ", value, "\n");
  79. }
  80.  
  81.  
  82. main (; cr, aFoo, aBar, x, y, i)
  83. {
  84.     print("\n*** Starting Tests ***\n\n");
  85.  
  86. //    TestGC();
  87.  
  88.     TestBoolOps();
  89.     TestRelOps();
  90.     TestAssignment();
  91.     TestIncDec();
  92.     TestStrings();
  93.     TestLoops();
  94.  
  95.     cr = "\n";
  96.  
  97.     x = 3;
  98.     if (x != 3)
  99.         Error("!=", "x", x);
  100.  
  101.     y = 7;
  102.     if (y != 7)
  103.         Error("!=", "y", y);
  104.  
  105.  
  106.     if (x * y != 21)
  107.         Error("*", "x * y", x * y);
  108.  
  109. //    aFoo = new Foo(x, y);
  110.     aBar = new Bar(2, 4, 6);
  111.     aFoo = new Bar(x, y, x + y);
  112.  
  113.     TestGC();
  114.  
  115.  
  116.     print("\tTesting object stuff\n");
  117.  
  118.     if (aFoo.GetA() == x)
  119.         ;
  120.     else
  121.         Error1("==", aFoo);
  122.  
  123.     if (aFoo->GetA() != x)
  124.         Error1("!=", aFoo);
  125.  
  126.     if (aFoo->GetB() != y)
  127.         Error2("!=", aFoo);
  128.  
  129.     if (aFoo.GetA() + aFoo.GetB() != x + y)
  130.         Error12("+", aFoo, aFoo.GetA() + aFoo.GetB());
  131.  
  132.     if (aFoo->GetA() - aFoo->GetB() != x - y)
  133.         Error12("-", aFoo, aFoo->GetA() - aFoo->GetB());
  134.  
  135.     if (aFoo->GetA() * aFoo->GetB() != x * y)
  136.         Error12("*", aFoo, aFoo->GetA() * aFoo->GetB());
  137.  
  138.     if (aFoo->GetB() / aFoo->GetA() != y / x)
  139.         Error12("/", aFoo, aFoo->GetB() / aFoo->GetA());
  140.  
  141.     if (aFoo->GetC() != x + y)
  142.         print("GetC() = ", aFoo->GetC(), "\n");
  143.  
  144.     OK();
  145.  
  146.     print("*** Finished Tests ***\n");
  147. }
  148.  
  149.  
  150. OK ()
  151. {
  152.     print("\tOK\n\n");
  153. }
  154.  
  155.  
  156. //
  157. // Test boolean operators
  158. //
  159. TestBoolOps ( ; x, y, cr)
  160. {
  161.     print("\tTesting boolean operators\n");
  162.  
  163.     cr = "\n";
  164.     x = 1234567;
  165.     y = 7654321;
  166.  
  167.     if ((x & y) != 1098369)
  168.         print(x, " & ", y, " = ", x & y, cr);
  169.  
  170.     if ((x | y) != 7790519)
  171.         print(x, " | ", y, " = ", x | y, cr);
  172.  
  173.     if ((x ^ y) != 6692150)
  174.         print(x, " ^ ", y, " = ", x ^ y, cr);
  175.  
  176.     if (~x != -1234568)
  177.         print("~", x, " = ", ~x, cr);
  178.  
  179.     if ((x << 11) != -1766574080)
  180.         print(x, " << ", 11, " = ", x << 11, cr);
  181.  
  182.     if ((y >> 5) != 239197)
  183.         print(y, " >> ", 5, " = ", y >> 5, cr);
  184.  
  185.     if (!x)
  186.         print("!", x, " = ", !x, cr);
  187.  
  188.     if (!nil)
  189.         ;
  190.     else
  191.         print("NOT !nil = ", !nil, cr);
  192.  
  193.     OK();
  194. }
  195.  
  196.  
  197. //
  198. // Test relational operators
  199. //
  200. TestRelOps ( ; x, y, cr)
  201. {
  202.     print("\tTesting relational operators\n");
  203.  
  204.     cr = "\n";
  205.     x = 3;
  206.     y = 7;
  207.  
  208.     if (y == x)
  209.         print(x, " == ", y, cr);
  210.  
  211.     if (y != x)
  212.         ;
  213.     else
  214.         print("NOT ", x, " != ", y, cr);
  215.  
  216.     if (x > y)
  217.         print(x, " > ", y, cr);
  218.  
  219.     if (x >= y)
  220.         print(x, " >= ", y, cr);
  221.  
  222.     if (y < x)
  223.         print(x, " < ", y, cr);
  224.  
  225.     if (y <= x)
  226.         print(x, " <= ", y, cr);
  227.  
  228.     OK();
  229. }
  230.  
  231.  
  232. class TGarbage : Bar {    // a derived class
  233.     member1, member2, member3, member4, member5;
  234. }
  235.  
  236. TGarbage::TGarbage (aa, bb, cc)
  237. {
  238.     member1 = newvector(5);
  239.     return Bar(aa, bb, cc);
  240. }
  241.  
  242.  
  243. //
  244. // Test garbage collection
  245. //
  246. TestGC ( ; i, x)
  247. {
  248.     print("\tTesting garbage collection\n");
  249.  
  250.     for (i = 1; i <= 2000; ++i) {
  251.         if ((i % 10) == 0)
  252.             print(i / 10, (i % 100) == 0 ? "\n" : " ");
  253. //        if ((i >= 730 && i <= 740) || (i >= 1460 && i <= 1470))
  254. //            print(".");
  255.         x = new Bar(i, i + 1, i + i);
  256.     }
  257.  
  258.     print("\tPart 2\n");
  259.  
  260.         // i ≤ 716 is the maximum that does not produce a
  261.         // recursion error (MacBob 1.0ß2 & BBEdit Lite 3.0)
  262.     for (i = 1; i <= 716; ++i)
  263.         x = new TGarbage(x, i + 1, i + i);
  264.  
  265.     print("\tPart 3\n");
  266.  
  267.     gc();
  268.     x = nil;
  269.     gc();
  270.  
  271.     OK();
  272. }
  273.  
  274.  
  275. //
  276. // Test assignment operators
  277. //
  278. TestAssignment ( ; x, y, z, cr)
  279. {
  280.     cr = "\n";
  281.     print("\tTesting assignment operators\n");
  282.  
  283.     x = 1234;
  284.     y = 4567;
  285.  
  286.     z = x; z += y;
  287.     if (z != x + y)
  288.         print("z += y = ", z, cr);
  289.  
  290.     z = x; z -= y;
  291.     if (z != x - y)
  292.         print("z -= y = ", z, cr);
  293.  
  294.     z = x; z *= y;
  295.     if (z != x * y)
  296.         print("z *= y = ", z, cr);
  297.  
  298.     z = x; z /= y;
  299.     if (z != x / y)
  300.         print("z /= y = ", z, cr);
  301.  
  302.     z = x; z %= y;
  303.     if (z != x % y)
  304.         print("z %= y = ", z, cr);
  305.  
  306.     z = x; z &= y;
  307.     if (z != (x & y))
  308.         print("z &= y = ", z, cr);
  309.  
  310.     z = x; z |= y;
  311.     if (z != (x | y))
  312.         print("z |= y = ", z, cr);
  313.  
  314.     z = x; z ^= y;
  315.     if (z != (x ^ y))
  316.         print("z ^= y = ", z, cr);
  317.  
  318.     OK();
  319. }
  320.  
  321.  
  322. //
  323. // Test increment/decrement operators
  324. //
  325. TestIncDec ( ; x, cr)
  326. {
  327.     cr = "\n";
  328.     print("\tTesting increment/decrement operators\n");
  329.  
  330.     x = 1234;
  331.  
  332.     if (++x != x)
  333.         print("++x = ", x, cr);
  334.  
  335.     if (--x != x)
  336.         print("--x = ", x, cr);
  337.  
  338.     if (x++ != x - 1)
  339.         print("x++ = ", x, cr);
  340.  
  341.     if (x-- != x + 1)
  342.         print("x-- = ", x, cr);
  343.  
  344.     OK();
  345. }
  346.  
  347.  
  348. //
  349. // Test strings
  350. //
  351. TestStrings ( ; x, y, i, cr)
  352. {
  353.     cr = "\n";
  354.     print("\tTesting strings\n");
  355.  
  356.     x = "Testing!";
  357.     y = "!gnitseT";
  358.  
  359.     for (i = 0; i < sizeof(x); ++i)
  360.         if (x[i] != y[sizeof(y) - i - 1])
  361.             print("x[", i, "] = ", x[i], cr);
  362.  
  363.     OK();
  364. }
  365.  
  366.  
  367. //
  368. // Test loops
  369. //
  370. TestLoops ( ; x, y, i, cr)
  371. {
  372.     cr = "\n";
  373.     print("\tTesting loops\n");
  374.  
  375.     print("\t\tfor\n");
  376.     x = -10;
  377.     y = 10;
  378.     for (i = x; i < y; ++i) {
  379.         if (i != x)
  380.             print("i = ", i, ",  x = ", x, cr);
  381.         ++x;
  382.     }
  383.  
  384.     print("\t\twhile\n");
  385.     x = -10;
  386.     i = x;
  387.     while (i < y)
  388.         ++i;
  389.  
  390.     print("\t\tdo-while\n");
  391.     i = x;
  392.     do
  393.         ++i;
  394.     while (i < y);
  395.  
  396.     OK();
  397. }
  398.  
  399.  
  400.